﻿/*
 * File: GamePlayBaseScene_LoadContent.cs
 * Author: Gourav Das
 * Purpose: Load content gameplay
 * Created: June 19 2011
 */

using System;
using System.Linq;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System.Threading;
using System.IO;
using _22yards._2D;
using _22yards.Scenes.Helpers;
using _22yards.Core;
using _22yards;
using _22yards.EnumDeclarations;
using _22yards.ThreeD;
using _22yards.Helpers;
using Komires.MataliPhysics;

namespace _22yards.Scenes
{
    /// <summary>
    /// This is a scene that implements the 2D Gameplay screen
    /// </summary>
    partial class GamePlayBaseScene : Scene
    {
        /// <summary>
        /// Load graphics content for the game.
        /// </summary>
        public override void LoadContent()
        {
            if (content == null)
                content = new ContentManager(sceneEngine.Game.Services, "22yardsContent");

            SunlightDir = new Vector3(-1f, -1f, 1f);

            Load2DSceneContent();
            Load3DSceneContent();

            SetModelEffMaterialNTexture2D();
            SetModelEffMaterialNTexture3D();

            //////////////////////////
            ////////////////////////////
            //One time Blur calculations
            ////////////////////////////
            //////////////////////////
            FillGaussianBlurVariables(ref weights, ref offsetsHoriz, ref offsersVert);

            Thread.Sleep(3000);
        }

        /// <summary>
        /// Sets the model eff material N texture.
        /// </summary>
        private void SetModelEffMaterialNTexture2D()
        {
            #region Lighting and Texture for 3D models

            LightingMaterial mat1RightSideBar = new LightingMaterial();
            mat1RightSideBar.LightDirection = new Vector3(-0.7f, 0f, 0.8f);
            mat1RightSideBar.SpecularPower = 0.5f;
            mat1RightSideBar.Shininess = 0.5f;
            mat1RightSideBar.AmbientIntensity = 1.0f;
            mat1RightSideBar.AmbientColor = new Vector3(0.7f, 0.7f, 0.7f);
            mat1RightSideBar.DiffuseColor = new Vector3(0.7f, 0.7f, 0.7f);
            //Set effects to right side model
            rightSideBarModel.SetModelMeshEffect(ColorNormalGlowEff, true, mat1RightSideBar, "frontside",
                LoadTextureFromFile(@"22yardsContent/TwoDEE/Models/RightSideBar/BarInfoSlatefront.png"),
                LoadTextureFromFile(@"22yardsContent/TwoDEE/Models/RightSideBar/BarInfoSlatefront Bump_NRM.png"),
                LoadTextureFromFile(@"22yardsContent/TwoDEE/Models/RightSideBar/BarInfoSlatefront SPEC.png"),
                LoadTextureFromFile(@"22yardsContent/TwoDEE/Models/RightSideBar/BarInfoSlatefrontHeight.png"));
            rightSideBarModel.SetModelMeshEffect(ColorNormalGlowEff, true, mat1RightSideBar, "backside",
                LoadTextureFromFile(@"22yardsContent/TwoDEE/Models/RightSideBar/BarInfoSlaterear.png"),
                LoadTextureFromFile(@"22yardsContent/TwoDEE/Models/RightSideBar/BarInfoSlaterear NRM.png"),
                LoadTextureFromFile(@"22yardsContent/TwoDEE/Models/RightSideBar/BarInfoSlaterear SPEC.png"),
                LoadTextureFromFile(@"22yardsContent/TwoDEE/Models/RightSideBar/BarInfoSlaterearHeight.png"));
            rightSideBarModel.SetModelMeshEffect(ColorNormalGlowEff, true, mat1RightSideBar, "screen",
                TextureScreen,
                TextureScreenNormal,
                TextureScreenSpec,
                LoadTextureFromFile(@"22yardsContent/TwoDEE/Models/RightSideBar/screenHeight.png"));

            LightingMaterial matTeam1Logo = new LightingMaterial();
            matTeam1Logo.LightDirection = new Vector3(0f, 1f, 1f);
            matTeam1Logo.SpecularPower = 0;
            matTeam1Logo.Shininess = 0;
            matTeam1Logo.AmbientIntensity = 1.1f;
            matTeam1Logo.AmbientColor = new Vector3(0.7f, 0.7f, 0.7f);
            matTeam1Logo.DiffuseColor = new Vector3(0.7f, 0.7f, 0.7f);
            //Set effects for two logos
            Team1Logo.SetModelMeshEffect(ColorNormalGlowEff, true, matTeam1Logo, "logomesh",
                Team1LogoTexture,
                Team1LogoNormal,
                Team1LogoSpec);

            LightingMaterial matTeam2Logo = new LightingMaterial();
            matTeam2Logo.LightDirection = new Vector3(0f, 1f, 1f);
            matTeam2Logo.SpecularPower = 0;
            matTeam2Logo.Shininess = 0;
            matTeam2Logo.AmbientIntensity = 1.1f;
            matTeam2Logo.AmbientColor = new Vector3(0.7f, 0.7f, 0.7f);
            matTeam2Logo.DiffuseColor = new Vector3(0.7f, 0.7f, 0.7f);

            Team2Logo.SetModelMeshEffect(ColorNormalGlowEff, true, matTeam2Logo, "logomesh",
                Team2LogoTexture,
                Team2LogoNormal,
                Team2LogoSpec);


            LightingMaterial matPFBatsman = new LightingMaterial();
            matPFBatsman.LightDirection = new Vector3(-0.4f, -1f, 0.5f);
            matPFBatsman.SpecularPower = 0;
            matPFBatsman.Shininess = 0;
            matPFBatsman.AmbientIntensity = 0.3f;
            //Inintial rendering of info for batsman;
            Game22Yards.matchPlayEngine.match.GetBatsmenOnCrease(ref BatsmenOnCrease);
            photoFrames[0].PlayerID = BatsmenOnCrease[0];
            photoFrames[1].PlayerID = BatsmenOnCrease[1];
            RenderBatsmanInfoOnInfoTexture(ref texPFBattingTeamPlayer1Info, photoFrames[0], ref rndTargetBatsman1Info);
            RenderBatsmanInfoOnInfoTexture(ref texPFBattingTeamPlayer2Info, photoFrames[1], ref rndTargetBatsman2Info);
            photoFrames[0].SetModelMeshEffect(simpleEff, true, matPFBatsman, "playerinfomesh", texPFBattingTeamPlayer1Info);
            photoFrames[1].SetModelMeshEffect(simpleEff, true, matPFBatsman, "playerinfomesh", texPFBattingTeamPlayer2Info);
            RenderBatsManScoreOnScoreTexture(ref texPFBattingTeamPlayer1Score, photoFrames[0], ref rndTargetBatsman1Score);
            RenderBatsManScoreOnScoreTexture(ref texPFBattingTeamPlayer2Score, photoFrames[1], ref rndTargetBatsman2Score);
            photoFrames[0].SetModelMeshEffect(simpleEff, true, matPFBatsman, "playerscoremesh", texPFBattingTeamPlayer1Score);
            photoFrames[1].SetModelMeshEffect(simpleEff, true, matPFBatsman, "playerscoremesh", texPFBattingTeamPlayer2Score);

            LightingMaterial matPFBowler = new LightingMaterial();
            matPFBowler.LightDirection = new Vector3(-0.4f, -1f, 0.5f);
            matPFBowler.SpecularPower = 0;
            matPFBowler.Shininess = 0;
            matPFBowler.AmbientIntensity = 0.3f;
            //Initial rendering of info for bowler
            Game22Yards.matchPlayEngine.match.GetCurrentBowlers(ref CurrentBowlers);
            photoFrames[2].PlayerID = CurrentBowlers[0];
            photoFrames[3].PlayerID = CurrentBowlers[1];
            RenderBowlerInfoOnInfoTexture(ref texPFBowlingTeamPlayer1Info, photoFrames[2], ref rndTargetBowler1Info);
            RenderBowlerInfoOnInfoTexture(ref texPFBowlingTeamPlayer2Info, photoFrames[3], ref rndTargetBowler2Info);
            photoFrames[2].SetModelMeshEffect(simpleEff, true, matPFBowler, "playerinfomesh", texPFBowlingTeamPlayer1Info);
            photoFrames[3].SetModelMeshEffect(simpleEff, true, matPFBowler, "playerinfomesh", texPFBowlingTeamPlayer2Info);
            RenderBowlerScoreOnScoreTexture(ref texPFBowlingTeamPlayer1Score, photoFrames[2], ref rndTargetBowler1Score);
            RenderBowlerScoreOnScoreTexture(ref texPFBowlingTeamPlayer2Score, photoFrames[3], ref rndTargetBowler2Score);
            photoFrames[2].SetModelMeshEffect(simpleEff, true, matPFBowler, "playerscoremesh", texPFBowlingTeamPlayer1Score);
            photoFrames[3].SetModelMeshEffect(simpleEff, true, matPFBowler, "playerscoremesh", texPFBowlingTeamPlayer2Score);

            #endregion

            //Set initial texture of phone model based on screen type
            if (SubScreenState == (short)TwoDEESubScreenState.Summary)
            {
                Draw2DSceneSummary(ref TextureSummaryScreen);
                rightSideBarModel.SetModelMeshColorMapTextureMaterial("screen", TextureSummaryScreen);
            }
            if (SubScreenState == (short)TwoDEESubScreenState.ScoreCardView)
            {
                Draw2DSceneScoreCardView(ref TextureScoreCardScreen);
                rightSideBarModel.SetModelMeshColorMapTextureMaterial("screen", TextureScoreCardScreen);
            }

            SetTextureForPlayerRemainingBatsmen();
            SetTextureForPlayerRemainingBowlers();
        }

        /// <summary>
        /// Sets the model eff material N texture.
        /// </summary>
        private void SetModelEffMaterialNTexture3D()
        {
            #region Lighting and Texture for 3D models

            LightingMaterial mat1Ground = new LightingMaterial();
            mat1Ground.LightDirection = SunlightDir;// new Vector3(-0.4f, 1f, -0.5f);
            mat1Ground.LightColor = new Vector3(1f, 1f, 1f);
            mat1Ground.SpecularPower = 0;
            mat1Ground.Shininess = 2;
            mat1Ground.AmbientIntensity = 1.5f;
            mat1Ground.AmbientColor = new Vector3(0.75f, 0.7f, 0.7f);
            mat1Ground.DiffuseColor = new Vector3(0.75f, 0.7f, 0.7f);
            //Set effects to stadium model
            stadia.SetModelMeshEffect(ColorNormalGlowEff, true, mat1Ground, "ground",
                TexGround,
                TexGroundNormal,
                TexGroundSpec);
            stadia.SetModelMeshEffect(ColorNormalGlowEff, true, mat1Ground, "pitch",
                TexGroundPitch,
                TexGroundPitchNormal,
                TexGroundPitchSpec);
            stadia.SetModelMeshEffect(ColorNormalGlowEff, true, mat1Ground, "rope",
                TexRope,
                TexRopeNormal,
                TexRopeSpec);

            LightingMaterial mat1Pitch = new LightingMaterial();
            mat1Pitch.LightDirection = SunlightDir;// new Vector3(-0.4f, 1f, -0.5f);
            mat1Pitch.LightColor = new Vector3(1f, 1f, 1f);
            mat1Pitch.SpecularPower = 0;
            mat1Pitch.Shininess = 0;
            mat1Pitch.AmbientIntensity = 1.5f;
            mat1Pitch.AmbientColor = new Vector3(0.75f, 0.7f, 0.7f);
            mat1Pitch.DiffuseColor = new Vector3(0.75f, 0.7f, 0.7f);
            Pitch.SetModelMeshEffect(ColorNormalGlowEff, true, mat1Pitch, "pitch",
                TexPitch,
                TexPitchNormal,
                TexPitchSpec,
                TexPitchHeight);

            LightingMaterial mat1Stumps = new LightingMaterial();
            mat1Stumps.LightDirection = SunlightDir;// new Vector3(-0.4f, 1f, -0.5f);
            mat1Stumps.SpecularPower = 0;
            mat1Stumps.Shininess = 0;
            mat1Stumps.AmbientIntensity = 1.2f;
            mat1Stumps.AmbientColor = new Vector3(0.7f, 0.7f, 0.7f);
            mat1Stumps.DiffuseColor = new Vector3(0.7f, 0.7f, 0.7f);
            //Set effects to Stump model
            LeftStumpFarEnd.SetModelMeshEffect(ColorNormalGlowEff, true, mat1Stumps, "sidestump",
                TexSideStump,
                TexSideStumpNormal,
                TexSideStumpSpec);
            RightStumpFarEnd.SetModelMeshEffect(ColorNormalGlowEff, true, mat1Stumps, "sidestump",
                TexSideStump,
                TexSideStumpNormal,
                TexSideStumpSpec);
            CenterStumpFarEnd.SetModelMeshEffect(ColorNormalGlowEff, true, mat1Stumps, "sidestump",
                TexCenterStump,
                TexCenterStumpNormal,
                TexCenterStumpSpec);
            LeftStumpNearEnd.SetModelMeshEffect(ColorNormalGlowEff, true, mat1Stumps, "sidestump",
                TexSideStump,
                TexSideStumpNormal,
                TexSideStumpSpec);
            RightStumpNearEnd.SetModelMeshEffect(ColorNormalGlowEff, true, mat1Stumps, "sidestump",
                TexSideStump,
                TexSideStumpNormal,
                TexSideStumpSpec);
            CenterStumpNearEnd.SetModelMeshEffect(ColorNormalGlowEff, true, mat1Stumps, "sidestump",
                TexCenterStump,
                TexCenterStumpNormal,
                TexCenterStumpSpec);

            LightingMaterial mat1Bails = new LightingMaterial();
            mat1Bails.LightDirection = SunlightDir;// new Vector3(-0.4f, 1f, -0.5f);
            mat1Bails.SpecularPower = 0;
            mat1Bails.Shininess = 0;
            mat1Bails.AmbientIntensity = 1.2f;
            mat1Bails.AmbientColor = new Vector3(0.7f, 0.7f, 0.7f);
            mat1Bails.DiffuseColor = new Vector3(0.7f, 0.7f, 0.7f);
            //Set effects to Stump model
            LeftBailFarEnd.SetModelMeshEffect(ColorNormalGlowEff, true, mat1Bails, "bail",
                TexBail,
                TexBailNormal,
                TexBailSpec);
            RightBailFarEnd.SetModelMeshEffect(ColorNormalGlowEff, true, mat1Bails, "bail",
                TexBail,
                TexBailNormal,
                TexBailSpec);
            LeftBailNearEnd.SetModelMeshEffect(ColorNormalGlowEff, true, mat1Bails, "bail",
                TexBail,
                TexBailNormal,
                TexBailSpec);
            RightBailNearEnd.SetModelMeshEffect(ColorNormalGlowEff, true, mat1Bails, "bail",
                TexBail,
                TexBailNormal,
                TexBailSpec);


            LightingMaterial mat1Balls = new LightingMaterial();
            mat1Balls.LightDirection = SunlightDir;// new Vector3(-0.4f, 1f, -0.5f);
            mat1Balls.SpecularPower = 0.5f;
            mat1Balls.Shininess = 0.5f;
            mat1Balls.AmbientIntensity = 1.2f;
            mat1Balls.AmbientColor = new Vector3(0.7f, 0.7f, 0.7f);
            mat1Balls.DiffuseColor = new Vector3(0.7f, 0.7f, 0.7f);
            //Set effects to Stump model
            KBRedBall.SetModelMeshEffect(ColorNormalGlowEff, true, mat1Balls, "ball",
                TexKBRedBall,
                TexKBRedBallNormal,
                TexKBRedBallSpec);

            #endregion
        }
        /// <summary>
        /// Unload graphics and non-graphics content used by the game.
        /// </summary>
        public override void UnloadContent()
        {
            content.Unload();
        }
    }
}
